#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int arrayNesting(vector<int>& nums) {
        vector<int> tmp;
        int res=0;
        int idx=0;
        for(int i=0;i<nums.size();i++){
            idx=nums[i];
            cout<<"tmp:"<<idx<<" ";
            tmp.push_back(idx);
            while(true){
                cout<<nums[idx]<<endl;
                if(find(tmp.begin(),tmp.end(),nums[idx])!=tmp.end()){
                    if(tmp.size()>res){
                        res=tmp.size();
                    }
                    break;
                }else{
                    idx=nums[idx];
                    cout<<idx<<" ";
                    tmp.push_back(idx);
                }
                if(tmp.size()==nums.size()){
                    break;
                }
            }
            cout<<endl;
            tmp.clear();
        }
        return res;
    }
};

int main(){
    Solution s;
    vector<int> vec{5,4,0,3,1,6,2};
    for(auto& i:vec){
        cout<<i<<" ";
    }
    cout<<endl;
    cout<<s.arrayNesting(vec)<<endl;
    return 0;
}